<template>
<div>
    <div class="box-mid">
        <el-button v-hasPermission="'log:delete'" type="primary" @click="remove">删除</el-button>
        <el-button v-hasPermission="'log:export'" v-preventReClick="2000" @click="$emit('export')">导出Excel</el-button>
    </div>
    <div class="box-table">
        <el-table :data="tableData.rows" tooltip-effect="dark" v-loading="tableData.loading" @selection-change="selectedChange">
            <template slot="empty">
                <no-data :types="tableData.isSearch">
                    <span>暂无数据</span>
                </no-data>
            </template>
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="username" label="操作人" show-overflow-tooltip></el-table-column>
            <el-table-column prop="operation" label="操作描述" show-overflow-tooltip></el-table-column>
            <el-table-column label="耗时" show-overflow-tooltip>
                <template slot-scope="scope">
                    <template v-if="scope.row.time<=500"><el-tag type="success">{{scope.row.time}}ms</el-tag></template>
                    <template v-else-if="scope.row.time<=1000"><el-tag type="info">{{scope.row.time}}ms</el-tag></template>
                    <template v-else-if="scope.row.time<=1500"><el-tag type="warning">{{scope.row.time}}ms</el-tag></template>
                    <template v-else><el-tag type="danger">{{scope.row.time}}ms</el-tag></template>
                </template>
            </el-table-column>
            <el-table-column prop="method" label="执行方法" show-overflow-tooltip></el-table-column>
            <el-table-column prop="params" label="方法参数" show-overflow-tooltip></el-table-column>
            <el-table-column prop="ip" label="IP地址" show-overflow-tooltip></el-table-column>
            <el-table-column prop="location" label="操作地点" show-overflow-tooltip></el-table-column>
            <el-table-column prop="createTime" label="操作时间" show-overflow-tooltip></el-table-column>
        </el-table>
    </div>
    <div class="box-pagenation" v-if="tableData.rows.length>0">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="tableData.currentPage" :page-size="tableData.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="tableData.total">
        </el-pagination>
    </div>
</div>
</template>

<script>
import NoData from '@/components/NoData'
export default {
    name: 'ProjectsTable',
    components: { NoData },
    props: {
        tableData: {
            type: Object,
            required: true
        }
    },
    data () {
        return {
            selected: []
        }
    },
    methods: {
        remove () {
            if (this.selected.length > 0) {
                this.confirmRemove()
            } else {
                this.$message.error('请先选择日志')
            }
        },
        confirmRemove () {
            this.$confirm('当您点击确定按钮后，这些记录将会被彻底删除，确定删除所选中的记录?', '提示', {
                confirmButtonText: '确 定',
                cancelButtonText: '取 消',
                type: 'warning'
            }).then(() => {
                let params = this.selected.map(v => v.id).join(',')
                this.$delete('/log', params).then(res => {
                    if (res) {
                        this.$message.success('删除成功')
                        this.$emit('flash', true)
                    }
                })
            }).catch(() => {})
        },
        selectedChange (val) {
            this.selected = val
        },
        handleCurrentChange (val) {
            this.tableData.currentPage = val
            this.$emit('flash')
        },
        handleSizeChange (val) {
            this.tableData.pageSize = val
            this.$emit('flash')
        }
    }
}
</script>
